package com.nike.amoeba.ui.frg

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.nike.amoeba.R
import com.nike.amoeba.adapter.AssetDetailsAdapter
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.databinding.FAssetTextBinding
import com.nike.amoeba.ext.init
import com.nike.amoeba.viewmodel.AssetModel
import com.nike.amoeba.viewmodel.StockTextModel
import com.nike.amoeba.weight.recyclerview.RecycleViewDivider
import com.nike.amoeba.weight.recyclerview.SpaceItemDecoration
import me.hgj.jetpackmvvm.base.appContext
import org.json.JSONArray
import org.json.JSONObject


/**
 * @Author nike
 * @Date 2023/8/2 14:18
 * @Description rfid 展示详情
 */
class StockTextFrg: BaseFragment<StockTextModel, FAssetTextBinding>() {

    var jsonArray = JSONArray()

    val adapter by lazy { activity?.let { AssetDetailsAdapter(it, jsonArray) } }

    var bean: String? = null
    var assetNo: String? = null

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            bean = it.getString("bean")
            assetNo = it.getString("assetNo")
        }
        setToolBarTitle(assetNo)
        mDatabind.swipeRefresh.isEnabled = false
        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter!!).let {
//            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(0f), ConvertUtils.dp2px(0f), true))
            it.addItemDecoration(
                RecycleViewDivider(
                    requireContext(),
                    LinearLayoutManager.HORIZONTAL,
                    1,
                    ContextCompat.getColor(requireContext(), R.color.gray_c9c9ca)
                )
            )

        }
        mViewModel.onRequestText(bean)
        val tagObt = JSONObject()
        tagObt.put("title", appContext.getString(R.string.detailed))

        adapter!!.notifyDataSetChanged()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listJsonArray.observe(viewLifecycleOwner, {
            var list = it as JSONArray
            for (i in 0 until list.length()) {
                val obj = list.optJSONObject(i)
                jsonArray.put(obj)
            }
            adapter!!.notifyDataSetChanged()
        })
    }

}